var createFunction = function(func) { //工厂类，抽象出function结构
    var Function = {
        type: '', 
        functionName: '',
        argArray: [],
        bodyArray: [],
        hasReturn: false
    };
    var functionName = function(func) { //拿到函数名称
        return func[1];
    }
    var argArray = function(func) { //拿到参数列表
        var argArray = [];
        var read = false;
        for (var key in func) {
            if (func[key] === "(") {
                read = true;
                continue;
            }
            if (func[key] === ")") {
                read = false;
                break;
            }
            if (read) {
                if (func[key] !== ',') {
                    argArray.push(func[key]);
                }
            }
        }
        return argArray;
    }
    var bodyArray = function(func) { //拿到函数体
        var bodyArray = [];
        var read = false;
        for (var key in func) {
            if (func[key] === '{' && read === false) {
                read = true;
                continue;
            }
            if (read) {
                bodyArray.push(func[key]);
            }
        }
        bodyArray.pop();
        return bodyArray;
    }
    var hasReturn = function(func) { //判断是否有return值
        var hasReturn = false;
        for (var key in func) {
            if (func[key] === 'return') {
                hasReturn = true;
                break;
            }
        }
        return hasReturn;
    }
    Function.type = 'function';
    Function.functionName = functionName(func);
    Function.argArray = argArray(func);
    Function.bodyArray = bodyArray(func);
    Function.hasReturn = hasReturn(func);
    return Function;
}
exports.getFunctionArray = function(words) {//用来获得所有的function对象，装在数组中
    var FunctionArray = [];//用来装函数对象
    var FunctionTemp = [];//用来暂时保存函数代码
    var isFuncNow = false; //是否读到了函数
    var leftBraceNum = 0; //左大括号数
    var rightBraceNum = 0; //右大括号数
    for (var key in words) {
        if (words[key] === 'function') {
            isFuncNow = true;
        }
        if (isFuncNow === true) {
            FunctionTemp.push(words[key]);
            if (words[key] === '{') {
                leftBraceNum++;
            }
            if (words[key] === '}') {
                rightBraceNum++;
            }
            if (leftBraceNum >= 1 && rightBraceNum === leftBraceNum) {
                isFuncNow = false;
                leftBraceNum = 0;
                rightBraceNum = 0;
                FunctionArray.push(createFunction(FunctionTemp));
                FunctionTemp = [];
                continue;
            }
        } 
    }
    return FunctionArray;
}
exports.getParsedWords = function(words){//用来获得去除function之后的代码词组
    var isFuncNow = false; //是否读到了函数
    var leftBraceNum = 0; //左大括号数
    var rightBraceNum = 0; //右大括号数
    var wordsArray = [];
    for (var key in words) {
        if (words[key] === 'function') {
            isFuncNow = true;
        }
        if (isFuncNow === true) {
            if (words[key] === '{') {
                leftBraceNum++;
            }
            if (words[key] === '}') {
                rightBraceNum++;
            }
            if (leftBraceNum >= 1 && rightBraceNum === leftBraceNum) {
                isFuncNow = false;
                leftBraceNum = 0;
                rightBraceNum = 0;
                continue;
            }
        }else{
        	wordsArray.push(words[key]);
        }
    }
    return wordsArray;
}